﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Globalization;
using System.Data.SqlClient;

public partial class ajax_updateproject : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["id_user"] != null && Session["permission"] != null
            && (DisplayEnum.DisplayPermission((int)(Session["permission"])).ToLower() == "người đăng ký" || DisplayEnum.DisplayPermission((int)(Session["permission"])).ToLower() == "quản lý"))
        {
            #region  check current user is enable to access current project
            SqlParameter[] listParams = new SqlParameter[3];
            //-- param 1: @userId
            int userId = Int32.Parse(Session["id_user"].ToString());
            SqlParameter param = new SqlParameter("@userId", userId);
            param.SqlDbType = SqlDbType.Int;
            param.Direction = ParameterDirection.Input;
            listParams[0] = param;

            //-- param 2: @projectId
            int id = Int32.Parse(Request["id"]);
            param = new SqlParameter("@projectId", id);
            param.SqlDbType = SqlDbType.Int;
            param.Direction = ParameterDirection.Input;
            listParams[1] = param;

            //-- param 3: @isAccess
            param = new SqlParameter();
            param.ParameterName = "@isAccess";
            param.SqlDbType = SqlDbType.Int;
            param.Direction = ParameterDirection.Output;
            listParams[2] = param;

            dal.ExecuteSP("proCheckAccessProject_GET", listParams);
            int enable = Int32.Parse(listParams[2].Value.ToString());
            if (enable == 0)
            {
                Response.Write("FAIL");
                return;
            }

            #endregion

            string name_project = Request["name"];
            string des_project = comm.to_sql(Request["des"], "string");
            string start = Request["start"];


            DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
            dtfi.ShortDatePattern = "dd/MM/yyyy";
            dtfi.DateSeparator = "/";
            if (start.Length > 0)
            {
                DateTime dateStart = Convert.ToDateTime(start, dtfi);
                start = comm.to_sql(dateStart.Day.ToString() + "/" + dateStart.Month + "/" + dateStart.Year, "date");
            }
            else
                start = "null";

            string end = Request["end"];
            if (end.Length > 0)
            {
                DateTime dateEnd = Convert.ToDateTime(end, dtfi);
                end = comm.to_sql(dateEnd.Day.ToString() + "/" + dateEnd.Month + "/" + dateEnd.Year, "date");
            }
            else
                end = "null";

            string priority = comm.to_sql(Request["priority"], "number");
            string isPublic = (Request["public"] == "true") ? "1" : "0";
            string projectId = comm.to_sql(Request["id"], "number");



            // check project is exist
            SqlParameter[] someparams = new SqlParameter[3];
            param = new SqlParameter();
            param.Value = Int32.Parse(Session["id_user"].ToString());
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@userId";
            param.Direction = ParameterDirection.Input;
            someparams[0] = param;

            param = new SqlParameter();
            param.Value = name_project;
            param.SqlDbType = SqlDbType.NVarChar;
            param.Size = 255;
            param.ParameterName = "@projectName";
            param.Direction = ParameterDirection.Input;
            someparams[1] = param;

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@projectId";
            param.Value = -1;
            param.Direction = ParameterDirection.Output;
            someparams[2] = param;


            dal.ExecuteSP("proCheckExistProject_GET", someparams);
            if (someparams[2].Value.ToString() == "" || ((int)(someparams[2].Value) == Int32.Parse(Request["id"])))
            {
                name_project = comm.to_sql(name_project, "string");
                string sql = " UPDATE project " +
                             " SET name=" + name_project + ",start=" + start + ",[end]=" + end + ",priority=" + priority +
                                ",[public]=" + isPublic + ",description=" + des_project + 
                                ",createdby=" + comm.to_sql(userId.ToString(), "number") +
                             " WHERE id=" + projectId;
                //Response.Write(sql);
                //return;
                if (dal.Execute(sql) == 1)
                    Response.Write("OK");
                else
                    Response.Write("FAIL");
                return;
            }
            else if ((int)(someparams[2].Value) != Int32.Parse(Request["id"]))
            {
                Response.Write("DUPLICATE");
            }
            else
                Response.Write("FAIL");
        }
    }
}
